// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/platform/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_pickup = require("./accountBulkstockListing.css");
	var databind = require("databind");
	var financeId;
	var dialog = require("dialog");
	var inventoryState;
	var html_updateWaybillNumber = require("./html_updateWaybillNumber.html");
	var inventoryId = [];
	var remark;
	var productType;
	var tools = require("tools");
	var caseSizeMap = new tools.Map();
	var checks = [];
	var storage = window.sessionStorage; 

	var init = function(page) {
		productType = page.params.productType;
		financeId = storage.getItem("financeId");
		if(financeId==null){
			financeId = page.params.financeId;
		}else{
			storage.removeItem("financeId");
			$("#accountBulkstockListing .footButton").remove();
		}
		if(page.params==null){
			page.params="";
		}
		caseSizeMap.toString = function() {
			var str = "";
			for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
				if (this.container[keys[i]] == 0) {
					continue;
				}
				str = str  + this.container[keys[i]] + "X" + keys[i] + "<br/>";
			}
			return (str == "" ? "箱体情况：无" : "箱体情况：</br>" + str);
		}
		
		inventoryState = page.params.inventoryState;
		
		inquireFinanceContainerInventoryPageList(inventoryState, financeId, null, null, null);
		
		$("#accountBulkstockListing .caseNumber").keyup(function(){
			inquireFinanceContainerInventoryPageList(inventoryState, financeId, $("#accountBulkstockListing .caseNumber").val(), $("#accountBulkstockListing .waybillNumber").val(), null);
		});
		
		$("#accountBulkstockListing .waybillNumber").keyup(function(){
			inquireFinanceContainerInventoryPageList(inventoryState, financeId, $("#accountBulkstockListing .caseNumber").val(), $("#accountBulkstockListing .waybillNumber").val(), null);
		});
		
		$("#accountBulkstockListing .pickupState").unbind("click").click(function(){
			inquireFinanceContainerInventoryPageList(inventoryState, financeId, $("#accountBulkstockListing .caseNumber").val(), $("#accountBulkstockListing .waybillNumber").val(), $(this).attr("value"));
		});
		
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}
		
		//全选
		$("#accountBulkstockListing .checkedAll").unbind("click").click(function(){
			caseSizeMap.put("10GP", 0);
			caseSizeMap.put("20GP", 0);
			caseSizeMap.put("40GP", 0);
			caseSizeMap.put("48GP", 0);
			caseSizeMap.put("10HQ", 0);
			caseSizeMap.put("20HQ", 0);
			caseSizeMap.put("40HQ", 0);
			caseSizeMap.put("48HQ", 0);
			caseSizeMap.put("10RH", 0);
			caseSizeMap.put("20RH", 0);
			caseSizeMap.put("40RH", 0);
			caseSizeMap.put("48RH", 0);
			
			if ($(this).prop("checked")) {
				$("#accountBulkstockListing .checked").prop("checked", true);
			} else {
				$("#accountBulkstockListing .checked").prop("checked", false);
			}
			checks = [];
			$.each($("#accountBulkstockListing .checked:checked"), function() {
				statisticalCaseSize(caseSizeMap, $.trim($(this).parents("tr").find(".caseSize").html()));
				checks.push($(this).parents("tr").attr("inventoryId"));
			});
			$(".case_info").html(caseSizeMap.toString());
		});
		
		$("#updateWaybillNumber").unbind("click").click(function(){
			$("#popdialog").html(html_updateWaybillNumber);
			if (checks.length == 0) {
				base.alert(2, "请勾选需要变更物流运单号的箱！");
				return;
			}
			$("#dialog_updateWaybillNumber .inventories").html("");
			$.each(checks, function() {
				var str = '<input type="hidden" value="'+this+'" name="inventoryId" />'
				$("#dialog_updateWaybillNumber .inventories").append(str);
			});
			dialog.showDialog($("#dialog_updateWaybillNumber"));
			$("#dialog_updateWaybillNumber .sure_yes").unbind("click").click(function(){
				databind.submitDom($("#dialog_updateWaybillNumber"), function(){
					base.alert(1, "修改成功！");
					dialog.hideDialog($("#dialog_updateWaybillNumber"));
					router.route("bulkstockListing/accountBulkstockListing.html?financeId=" + financeId);
				});
			});
			
			$("#dialog_updateWaybillNumber .sure_no").unbind("click").click(function(){
				dialog.hideDialog($("#dialog_updateWaybillNumber"));
			});
		});
		
		//查询放箱状态
		$("#selectStatus").unbind("click").click(function(){
			$("#popdialog").html(html_updateWaybillNumber);
			if (checks.length == 0) {
				base.alert(2, "请勾选要查询的箱！");
				return;
			}
			
			var tmp = "";
			var caseNumbers = "";
			$.each($("#accountBulkstockListing .checked:checked"), function() {
				tmp = $(this).parents("tr").attr("waybillNumber");
				caseNumbers = $(this).parents("tr").attr("caseNumber");
				containerTaken(tmp, caseNumbers);
			});
		});
		
		//加锁
		$("#goldLocked").unbind("click").click(function(){
			
			var tmp = "";
			var caseNumbers = "";
			$.each($("#accountBulkstockListing .checked:checked"), function() {
				var waybillNumber = $(this).parents("tr").attr("waybillNumber");
				var caseNumber = $(this).parents("tr").attr("caseNumber");
				goldLocked(waybillNumber, caseNumber);
			});
		});
		
		//解锁
		$("#goldUnlocked").unbind("click").click(function(){
			$.each($("#accountBulkstockListing .checked:checked"), function() {
				var waybillNumber = $(this).parents("tr").attr("waybillNumber");
				var caseNumber = $(this).parents("tr").attr("caseNumber");
				goldUnlocked(waybillNumber, caseNumber);
			});
		});
		
		//箱动态
		$(".inventoryLog").unbind("click").click(function(){
			tools.inquireContainerInventoryLogList($(this).parents("tr").attr("inventoryId"));
		});
	};
	
	var inquireFinanceContainerInventoryPageList = function(inventoryState, financeId, caseNumber, waybillNumber, pickupState){
		var tpl = $("#bulkstockListingTpl");
		if (productType == 3 || productType == 5 || productType == 6) {
			tpl = $("#bulkstockListingTpl1");
		}
		
		tools.loadList("inquireFinanceContainerInventoryList.json", $("#bulkstockListingBody"), tpl, {caseNumber: caseNumber, waybillNumber: waybillNumber, financeId: financeId, inventoryState: inventoryState, pageSize:1000, inventoryLockState: false, pickupState: pickupState}, function(){
			$("#accountBulkstockListing .detainedDetail").unbind("click").click(function(){
				router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
			});
			
			$("#accountBulkstockListing .detail").unbind("click").click(function(){
				router.route("main/wareHouseDetail.html?caseNumber=" + $(this).parents("tr").attr("caseNumber") + "&financeNumber=" + $(this).parents("tr").attr("financeNumber"));
			});
			
			$("#accountBulkstockListing .inquireLogisticsDetailInfo").unbind("click").click(function(){
				router.route("logisticsInfo/inquireLogisticsInfoDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
			});
			
			//复选框
			$("#accountBulkstockListing .checked").unbind("click").click(function(){
				caseSizeMap.put("10GP", 0);
				caseSizeMap.put("20GP", 0);
				caseSizeMap.put("40GP", 0);
				caseSizeMap.put("48GP", 0);
				caseSizeMap.put("10HQ", 0);
				caseSizeMap.put("20HQ", 0);
				caseSizeMap.put("40HQ", 0);
				caseSizeMap.put("48HQ", 0);
				caseSizeMap.put("10RH", 0);
				caseSizeMap.put("20RH", 0);
				caseSizeMap.put("40RH", 0);
				caseSizeMap.put("48RH", 0);
				
				checks = [];
				$.each($("#accountBulkstockListing .checked:checked"), function() {
					statisticalCaseSize(caseSizeMap, $.trim($(this).parents("tr").find(".caseSize").html()));
					checks.push($(this).parents("tr").attr("inventoryId"));
				});
				$(".case_info").html(caseSizeMap.toString());
				if ($("#accountBulkstockListing .checked:checked").length == $("#accountBulkstockListing .checked").length) {
					$("#accountBulkstockListing .checkedAll").prop("checked", true);
				} else {
					$("#accountBulkstockListing .checkedAll").prop("checked", false);
				}
			});
			
			$("#accountBulkstockListing .shipInfo").unbind("click").click(function(){
				inquireShipInfo($(this).parents("tr").attr("waybillNumber"), $(this).parents("tr").attr("caseNumber"), 1, null);
			});
			
			$("#accountBulkstockListing .shipDynamic").unbind("click").click(function(){
				inquireShipInfo($(this).parents("tr").attr("waybillNumber"), $(this).parents("tr").attr("caseNumber"), 2, $(this).parents("tr").attr("inventoryId"));
			});
			
			$("#accountBulkstockListing #shipInfo").unbind("click").click(function(){
				var tr = $($("#accountBulkstockListing tr")[1]);
				if ($("#accountBulkstockListing tr").length > 1) {
					inquireShipInfo(tr.attr("waybillNumber"), tr.attr("caseNumber"), 2, tr.attr("inventoryId"));
				}
			});
		});
	};
	
	var statisticalCaseSize = function(m, c) {
		switch (c){
			case "10GP":
				m.put("10GP", m.get("10GP")*1 + 1);
				break;
			case "20GP":
				m.put("20GP", m.get("20GP")*1 + 1);
				break;
			case "40GP":
				m.put("40GP", m.get("40GP")*1 + 1);
				break;
			case "48GP":
				m.put("48GP", m.get("48GP")*1 + 1);
				break;
			case "10HQ":
				m.put("10HQ", m.get("10HQ")*1 + 1);
				break;
			case "20HQ":
				m.put("20HQ", m.get("20HQ")*1 + 1);
				break;
			case "40HQ":
				m.put("40HQ", m.get("40HQ")*1 + 1);
				break;
			case "48HQ":
				m.put("48HQ", m.get("48HQ")*1 + 1);
				break;
			case "10RH":
				m.put("10RH", m.get("10RH")*1 + 1);
				break;
			case "20RH":
				m.put("20RH", m.get("20RH")*1 + 1);
				break;
			case "40RH":
				m.put("40RH", m.get("40RH")*1 + 1);
				break;
			case "48RH":
				m.put("48RH", m.get("48RH")*1 + 1);
				break;
			default:
				break;
		}
	};
	
	//查询放箱状态
	var containerTaken = function(waybillNumber, caseNumber) {
		base.fnAjax({
			action: "containerTaken.json",
			async: false,
			oData: {
				waybillNumber: waybillNumber,
				caseNumber: caseNumber
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if ((data.datas[0].result == true) && (data.datas[0].data[0].attrs.cffinancelock == 1)) {
						$("tr[casenumber=" + caseNumber + "]").children("td.state").text("已加锁");
					} else {
						$("tr[casenumber=" + caseNumber + "]").children("td.state").text("未加锁");
					}
				} else {
					base.alert(3, "查询失败");
				}
			}
		});
	};
	
	//查询放箱状态
	var inquireShipInfo = function(waybillNumber, caseNumber, type, inventoryId) {
		base.fnAjax({
			action: "inquireShipInfo.json",
			async: false,
			oData: {
				waybillNumber: waybillNumber,
				caseNumber: caseNumber
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (type == 1) {
						window.open('http://222.79.247.164:18091/rjyingyan_yirun/html51/Bay/index.html?vessel='+window.btoa(encodeURIComponent(data.datas[0].type))+'&connum='+caseNumber);
					} else {
						if (data.datas[0].op_type == "6") {
							window.open('http://222.79.247.164:18091/rjyingyan_yirun/html51/vslMonitordetail.html?cm='+window.btoa(encodeURIComponent(data.datas[0].type))+'&mmsi='+data.datas[0].cfmmsi);
						} else {
							router.route("logisticsInfo/inquireLogisticsInfoDetail.html?inventoryId="+inventoryId+'&portname='+window.btoa(encodeURIComponent(data.datas[0].type)) + "&caseNumber=" + caseNumber + "&waybillNumber=" + waybillNumber);
//							window.open('http://222.79.247.164:18091/rjyingyan_yirun/html51/map.html?portname='+window.btoa(encodeURIComponent(data.datas[0].type)) + "&caseNumber=" + caseNumber + "&waybillNumber=" + waybillNumber);
						}
					}
				} else {
					base.alert(3, "正在陆地运输过程中");
				}
			}
		});
	};
	
	//加锁
	var goldLocked = function(waybillNumber, caseNumber) {
		base.fnAjax({
			action: "goldLocked.json",
			async: false,
			oData: {
				waybillNumber: waybillNumber,
				caseNumber: caseNumber
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0].result) {
//						inquireFinanceContainerInventoryPageList(inventoryState, financeId, null);
						base.alert(1, "加锁成功！");
					} else {
//						inquireFinanceContainerInventoryPageList(inventoryState, financeId, null);
						base.alert(2, "加锁失败！" + data.datas[0].message);
					}
				} else {
					base.alert(3, "查询失败");
				}
			}
		});
	};
	
	//解锁
	var goldUnlocked = function(waybillNumber, caseNumber) {
		base.fnAjax({
			action: "goldUnlocked.json",
			async: false,
			oData: {
				waybillNumber: waybillNumber,
				caseNumber: caseNumber
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0].result) {
//						inquireFinanceContainerInventoryPageList(inventoryState, financeId, null);
						base.alert(1, "解锁成功！");
					} else {
//						inquireFinanceContainerInventoryPageList(inventoryState, financeId, null);
						base.alert(2, "解锁失败！");
					}
				} else {
					base.alert(3, "查询失败");
				}
			}
		});
	};
	
	return {
		init: init
	};
});